package com.andox.fn.inf.rpc.rest.parser;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;

import com.andox.fn.inf.rpc.rest.proxy.ReferenceBean;


/**   
 * @ClassName:   ReferenceDefinitionParser   
 * @Description: TODO  
 * @author:      Andox
 * @date:        2019年7月29日 下午12:03:33   
 *     
 */
public class ReferenceDefinitionParser  extends AbstractContractDefinitionParser {
    private static final String BASE_URL_ATTRIBUTE = "baseUrl";
    private static final String TIME_OUT_ATTRIBUTE = "timeout";
    private static final String SERIALIZATION_ATTRIBUTE = "serialization";
    private static final String ONEWAY_ATTRIBUTE = "oneway";
    private static final String ASYNC_ATTRIBUTE = "async";
    
    @Override
    protected void doParseInternal(Element element, ParserContext parserContext,
                                   BeanDefinitionBuilder builder) {
        
        String baseUrl = element.getAttribute(BASE_URL_ATTRIBUTE);
//        if (!StringUtils.hasText(baseUrl))
//        	throw new RuntimeException("Invalid value of property baseUrl");
        	
        builder.addPropertyValue(BASE_URL_ATTRIBUTE, baseUrl);
        
        String timeout = element.getAttribute(TIME_OUT_ATTRIBUTE);
        if (StringUtils.hasText(timeout))
        	builder.addPropertyValue(TIME_OUT_ATTRIBUTE, timeout);
        
        String serialization = element.getAttribute(SERIALIZATION_ATTRIBUTE);
        if (StringUtils.hasText(serialization))
        	builder.addPropertyValue(SERIALIZATION_ATTRIBUTE, serialization);
        
        String async = element.getAttribute(ASYNC_ATTRIBUTE);
        if (StringUtils.hasText(async))
        	builder.addPropertyValue(ASYNC_ATTRIBUTE, async);
        
        String oneway = element.getAttribute(ONEWAY_ATTRIBUTE);
        if (StringUtils.hasText(oneway))
        	builder.addPropertyValue(ONEWAY_ATTRIBUTE, oneway);
        
        MethodDefinitionParser.parse(element, parserContext, builder);
    }

    @Override
    protected Class<?> getBeanClass(Element element) {
        return ReferenceBean.class;
    }

    @Override
    public String supportTagName() {
        return RestRpcNamespaceHandler.REFERENCE_TAG;
    }
}